package com.kefu.exam.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kefu.common.context.KefuContext;
import com.kefu.exam.mapper.ExamPaperSubjectMapper;
import com.kefu.exam.mapper.ExamSubjectAnswerMapper;
import com.kefu.exam.mapper.ExamSubjectMapper;
import com.kefu.exam.model.ExamPaperSubject;
import com.kefu.exam.model.ExamSubject;
import com.kefu.exam.model.ExamSubjectAnswer;
import com.kefu.exam.vo.ExamCascadeJumpInsertVo;
import com.kefu.exam.vo.ExamCascadeJumpSaveVo;
import com.kefu.exam.vo.ExamCascadeJumpSearchVo;
import icu.mhb.mybatisplus.plugln.core.JoinLambdaWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.kefu.common.service.AbstractKefuService;
import com.kefu.exam.mapper.ExamCascadeJumpMapper;
import com.kefu.exam.model.ExamCascadeJump;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xiaoyu
 * @date 2022/10/24
 */
@Service
public class ExamCascadeJumpService extends AbstractKefuService<ExamCascadeJump> {
    @Autowired
    protected ExamCascadeJumpMapper examCascadeJumpMapper;
    @Autowired
    protected ExamSubjectMapper examSubjectMapper;
    @Autowired
    protected ExamSubjectAnswerMapper examSubjectAnswerMapper;
    @Autowired
    protected ExamPaperSubjectMapper examPaperSubjectMapper;


    public void saveBatchCascadeJump(ExamCascadeJumpSaveVo entityList) {
        if (entityList.getJumpInsertVoList() == null && entityList.getJumpInsertVoList().size() <= 0) {
            examCascadeJumpMapper.delete(Wrappers.lambdaQuery(ExamCascadeJump.class)
                    .eq(ExamCascadeJump::getPaperId, entityList.getPaperId()));
        }
        examCascadeJumpMapper.delete(Wrappers.lambdaQuery(ExamCascadeJump.class)
                .eq(ExamCascadeJump::getPaperId, entityList.getPaperId()));
        if (entityList.getJumpInsertVoList() != null && entityList.getJumpInsertVoList().size() > 0) {
            for (ExamCascadeJumpInsertVo vo : entityList.getJumpInsertVoList()) {
                ExamCascadeJump examCascadeJump = vo.convert();
                if (examCascadeJump.getAnswerId().equals("无条件")) {
                    List<ExamSubjectAnswer> examSubjectAnswers = examSubjectAnswerMapper.selectList(Wrappers.lambdaQuery(ExamSubjectAnswer.class)
                            .eq(ExamSubjectAnswer::getSubjectId, examCascadeJump.getSubjectId()));
                    examSubjectAnswers.forEach(answer -> {
                        ExamCascadeJump jump = vo.convert();
                        jump.setAnswerId(answer.getAnswerId());
                        examCascadeJumpMapper.insert(jump);
                    });
                } else {
                    examCascadeJumpMapper.insert(examCascadeJump);
                }
            }
        }
    }


    public Page<ExamCascadeJump> searchLambdaPage(ExamCascadeJumpSearchVo searchVo) {
        Page<ExamCascadeJump> examCascadeJumpPage = super.searchLambdaPage(searchVo);
        JoinLambdaWrapper<ExamSubject> wrapper = new JoinLambdaWrapper<>(ExamSubject.class);
        wrapper.eq(ExamSubject::getTenantId, KefuContext.getKefuContext().getTenantId())
                .leftJoin(ExamPaperSubject.class, ExamPaperSubject::getSubjectId, ExamSubject::getSubjectId)
                .eq(ExamPaperSubject::getPaperId, searchVo.getPaperId());
        List<ExamSubject> subjectList = examSubjectMapper.joinSelectList(wrapper, ExamSubject.class);
        LambdaQueryWrapper<ExamSubjectAnswer> queryWrapper = new LambdaQueryWrapper<>();
        for (ExamCascadeJump jump : examCascadeJumpPage.getRecords()) {
            for (ExamSubject subject : subjectList) {
                if (jump.getSubjectId().equals(subject.getSubjectId())) {
                    jump.setSubjectContent(subject.getSubjectContent());
                }
                if (jump.getJumpSubject().equals(subject.getSubjectId())) {
                    jump.setJumpSubjectContent(subject.getSubjectContent());
                }
                queryWrapper.eq(ExamSubjectAnswer::getSubjectId, subject.getSubjectId())
                        .or();
            }
            List<ExamSubjectAnswer> answerList = examSubjectAnswerMapper.selectList(queryWrapper);
            for (ExamSubjectAnswer answer : answerList) {
                if (jump.getAnswerId().equals(answer.getAnswerId())) {
                    jump.setAnswerContent(answer.getAnswerContent());
                }
            }
        }
        LambdaQueryWrapper<ExamSubject> examSubjectLambdaQueryWrapperwrapper = new LambdaQueryWrapper<>();
        examCascadeJumpPage.getRecords().forEach(jump -> {
            wrapper.eq(ExamSubject::getSubjectId, jump.getSubjectId())
                    .or();
        });
        List<ExamSubject> subjects = examSubjectMapper.selectList(examSubjectLambdaQueryWrapperwrapper);
        List<ExamPaperSubject> examPaperSubjectList = examPaperSubjectMapper.selectList(Wrappers.lambdaQuery(ExamPaperSubject.class)
                .eq(ExamPaperSubject::getPaperId, searchVo.getPaperId())
                .orderBy(true, true, ExamPaperSubject::getSort));
        List<ExamCascadeJump> resultList = new ArrayList<>();
        examPaperSubjectList.forEach(paperSubject -> {
            examCascadeJumpPage.getRecords().forEach(jump -> {
                subjects.forEach(examSubject -> {
                    if (examSubject.getSubjectId().equals(jump.getSubjectId())) {
                        jump.setTypeId(examSubject.getTypeId());
                    }
                });
                if (jump.getSubjectId().equals(paperSubject.getSubjectId())) {
                    resultList.add(jump);
                }
            });
        });
        examCascadeJumpPage.setRecords(resultList);
        return examCascadeJumpPage;
    }
}